/* 
 *  Syntelos XB
 *  Copyright (C) 2006 John Pritchard.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307 USA
 */

package syntelos.xb.at;

/**
 * <p> Entry in {@link LineNumberTable}. </p>
 */
public final class Line
    extends syntelos.xb.Base
{
    public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/at","xat:line");

    /**
     * <p> Start offset PC. </p>
     */
    public final static class Start
        extends syntelos.xb.op.Operand.Branch
    {
        public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/at","xat:line.start");

        public Start(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
            super(doc,name);
        }
        public Start(org.w3c.dom.Document doc){
            this(doc,NAME);
        }
        public Start(){
            this(DOC_NIL);
        }

        protected Line getLine(){
            return (Line)this.getParentNode();
        }
        protected LineNumberTable getAttributeLineNumberTable(){
            return this.getLine().getAttributeLineNumberTable();
        }
        public Code getAttributeCode(){
            return this.getLine().getAttributeCode();
        }
        public LocalVariableTable getAttributeLocalVariableTable(){
            return this.getLine().getAttributeLocalVariableTable();
        }
        public syntelos.xb.op.Code getOpCode(){
            return this.getAttributeLineNumberTable().getOpCode();
        }
        public syntelos.xb.Method getMethod(){
            return this.getAttributeLineNumberTable().getMethod();
        }
    }

    /**
     * <p> Line number. </p>
     */
    public final static class Number
        extends syntelos.xb.op.Operand
    {
        public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/at","xat:line.number");

        public Number(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
            super(doc,name);
        }
        public Number(org.w3c.dom.Document doc){
            this(doc,NAME);
        }
        public Number(){
            this(DOC_NIL);
        }

        protected Line getLine(){
            return (Line)this.getParentNode();
        }
        protected LineNumberTable getAttributeLineNumberTable(){
            return this.getLine().getAttributeLineNumberTable();
        }
        public Code getAttributeCode(){
            return this.getLine().getAttributeCode();
        }
        public LocalVariableTable getAttributeLocalVariableTable(){
            return this.getLine().getAttributeLocalVariableTable();
        }
        public syntelos.xb.op.Code getOpCode(){
            return this.getAttributeLineNumberTable().getOpCode();
        }
        public syntelos.xb.Method getMethod(){
            return this.getAttributeLineNumberTable().getMethod();
        }
    }

    public Line(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
        super(doc,name);
    }
    public Line(org.w3c.dom.Document doc){
        this(doc,NAME);
    }
    public Line(){
        this(DOC_NIL);
    }

    protected LineNumberTable getAttributeLineNumberTable(){
        return (LineNumberTable)this.getParentNode();
    }
    public syntelos.xb.op.Code getOpCode(){
        return this.getAttributeLineNumberTable().getOpCode();
    }
    public syntelos.xb.Method getMethod(){
        return this.getAttributeLineNumberTable().getMethod();
    }
    public syntelos.xb.at.Code getAttributeCode(){
        return this.getAttributeLineNumberTable().getAttributeCode();
    }
    public syntelos.xb.at.LocalVariableTable getAttributeLocalVariableTable(){
        return this.getAttributeLineNumberTable().getAttributeLocalVariableTable();
    }
    public Start getStartOperand(){
        Start start = (Start)this.getChildByName("line.start");
        if (null == start){
            start = new Start();
            this.appendChild(start);
        }
        return start;
    }
    public int getStart(){
        return this.getStartOperand().getOperand();
    }
    public void setStart(int pc){
        this.getStartOperand().setOperand(pc);
    }
    public void setStart(syntelos.xb.Op op){
        this.getStartOperand().setOperand(op);
    }
    public Number getNumberOperand(){
        Number number = (Number)this.getChildByName("line.number");
        if (null == number){
            number = new Number();
            this.appendChild(number);
        }
        return number;
    }
    public int getNumber(){
        return this.getNumberOperand().getOperand();
    }
    public void setNumber(int number){
        this.getNumberOperand().setOperand(number);
    }

    public void readCF(syntelos.xb.IO.Uri src, syntelos.xb.CF.Reader reader)
        throws java.io.IOException
    {
        int pc_start = reader.readUnsignedShort();
        this.setStart(pc_start);
        int number = reader.readUnsignedShort();
        this.setNumber(number);
    }
    public void writeCF(syntelos.xb.IO.Uri dst, syntelos.xb.CF.Writer writer)
        throws java.io.IOException
    {
        int pc_start = this.getStart();
        writer.writeShort(pc_start);
        int number = this.getNumber();
        writer.writeShort(number);
    }
}
